package com.emergency.db.service.impl.staff;

import com.emergency.common.util.CommonUtils;
import com.emergency.common.util.IdWorker;
import com.emergency.db.dao.staff.StaffLocationDao;
import com.emergency.db.domain.staff.Staff;
import com.emergency.db.domain.staff.StaffLocation;
import com.emergency.db.service.staff.StaffLocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;


@Service
@Transactional
public class StaffLocationServiceImpl implements StaffLocationService {
    @Autowired
    private IdWorker idWorker;

    @Autowired
    private StaffLocationDao staffLocationDao;

    @Override
    public Long save(StaffLocation staffLocation) {
        if (CommonUtils.isNullOrEmpty(staffLocation.getId())) {
            staffLocation.setId(idWorker.nextId());
            staffLocation.setCreateTime(new Date());
        }
        staffLocationDao.save(staffLocation);
        return staffLocation.getId();
    }

    @Override
    public void deleteAllByStaff(Long id) {
        staffLocationDao.deleteAllByStaff(id);
    }

    //分页查询
    @Override
    public Page<StaffLocation> findList(Map searchMap, int page, int size, Long id) {
        Sort sort = Sort.by(Sort.Direction.DESC, "create_time");
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        return staffLocationDao.findAllByStaff(id, pageable);
    }

}
